from pipes import Template
import S_Draw
import K_Local_Search
from G_Initial import *
import I_GA_Util
import J_Fast_Sort
import M_Evaluate


class Djaya:

    def __init__(self):
        self.Max_Itertions = 1
        self.Init = Initial()  # 初始化类

    # 对每一代种群产生子父代混合种群
    def Melt_Population(self, Pop1, Pop2):
        print("模块：Djaya\t方法：Melt_Population(self, Pop1,Pop2):")
        Merge_Population = []
        for i in range(Pop1.Pop_Size):
            Merge_Population.append(Pop1.Population_Member[i])
        for i in range(Pop2.Pop_Size):
            Merge_Population.append(Pop2.Population_Member[i])

        return Merge_Population

    def Show(self, Pop):
        print("模块：Djaya\t方法：Show(self,Pop):")
        print(Pop.Nodominate_Ship)
        print(Pop.Unduplicated_Pareto_Num)
        print(Pop.Unduplicated_Pareto)
        print(Pop.Unduplicated_Pareto_Obj)

        # 打印每一层的非重复解的数目  打印每一层目标值非重复的

    def Show_Dup(self, Pop):
        print("模块：Djaya\t方法：Show_Dup(self,Pop):")
        # print("Nodominate_Ship",Pop.Nodominate_Ship)
        U_Individual = []
        U_Space = []
        levels = len(Pop.Nodominate_Ship)  # Pareto层数
        for i in range(levels):  # 遍历每一层
            Temp_U_Individual = []
            Temp_U_Space = []

            for j in Pop.Nodominate_Ship[i]:

                # print("%d %s "%(j,
                # Pop.Population_Member[j].Adaptability),end=" ")

                Temp = []
                Temp.append(Pop.Population_Member[j].Job_Chromosome.tolist())
                Temp.append(Pop.Population_Member[j].Machine_Chromosome.tolist())
                if Temp not in Temp_U_Individual:
                    Temp_U_Individual.append(Temp)
                if Pop.Population_Member[j].Adaptability not in Temp_U_Space:
                    Temp_U_Space.append(Pop.Population_Member[j].Adaptability)
            U_Individual.append(Temp_U_Individual)
            U_Space.append(Temp_U_Space)
            # print()

        print("Indiv:", end=" ")
        for i in range(levels):
            print("[%d/%d]" % (len(U_Individual[i]), len(Pop.Nodominate_Ship[i])), end=" ")
        print()
        print("Space:", end=" ")
        for i in range(levels):
            print("[%d/%d]" % (len(U_Space[i]), len(Pop.Nodominate_Ship[i])), end=" ")
        print()

    # 运行
    def Run(self, Init_Pop):
        print("模块：Djaya\t方法：Run(self,Init_Pop):")
        gen = 0
        Pop = Init_Pop  # 初始种群
        Map = Load_Process_Map()

        while gen < self.Max_Itertions:
            print("===============================================================================================",
                  gen + 1)
            self.Show_Dup(Pop)
            New_Pop = I_GA_Util.Update_Pop_DJaya(Pop, Map)  # 交叉

            for i in range(Pop.Pop_Size):
                New_Pop.Population_Member[i] = K_Local_Search.Local_Search_Djaya(New_Pop.Population_Member[i], Map)

            Prop = I_GA_Util.Get_Mutation_Prob(gen, self.Max_Itertions)
            I_GA_Util.Mutation_Pop_DJaya(New_Pop, Map, Prop)

            # 种群合并 Pareto 关系 生成下一代种群
            Merge_Population = self.Melt_Population(Pop, New_Pop)

            Nodominate_Sort = J_Fast_Sort.Fast_Nodominate_Sort(Merge_Population)

            Next_Population = []  # 从子父代混合种群中选取前200个作为下一代种群
            i = 0
            # 按照Parato等级依次添加
            while (len(Next_Population) + len(Nodominate_Sort[i])) <= Pop.Pop_Size:
                for j in Nodominate_Sort[i]:
                    Next_Population.append(Merge_Population[j])
                i = i + 1

            # for m in Nodominate_Sort[i]:    # 把最后一层先加上
            #     Next_Population.append(Merge_Population[m])
            # Next_Population = Next_Population[0:Pop.Pop_Size]   # 生成新种群

            Di = J_Fast_Sort.Modified_Crowding_Distance(Nodominate_Sort[i], Merge_Population)  # 拥挤度 未排序

            Temp_Choose = []  # 选解
            for j in range(len(Di)):
                Temp = []
                Temp.append(Di[j])
                Temp.append(Nodominate_Sort[i][j])
                Temp_Choose.append(Temp)

            Temp_Choose = sorted(Temp_Choose)

            Index = len(Temp_Choose) - 1
            while len(Next_Population) < Pop.Pop_Size:
                Next_Population.append(Merge_Population[Temp_Choose[Index][1]])

            Pop.Update_Pop(Next_Population)
            Pop.Determine_Nodominate_Ship()

            gen += 1

        return Pop

    # 运行
    def Run1(self, Init_Pop):
        print("模块：Djaya\t方法：Run1(self,Init_Pop):")
        gen = 0
        Pop = Init_Pop  # 初始种群
        Map = Load_Process_Map()

        while gen < self.Max_Itertions:
            print("===============================================================================================",
                  gen + 1)
            self.Show_Dup(Pop)
            New_Pop = I_GA_Util.Update_Pop_DJaya(Pop, Map)  # 交叉

            # for i in range(Pop.Pop_Size):
            #     New_Pop.Population_Member[i] = Local_Search.Local_Search_Djaya(New_Pop.Population_Member[i],Map)    

            Prop = I_GA_Util.Get_Mutation_Prob(gen, self.Max_Itertions)
            I_GA_Util.Mutation_Pop_DJaya(New_Pop, Map, Prop)

            # 种群合并 Pareto 关系 生成下一代种群
            Merge_Population = self.Melt_Population(Pop, New_Pop)

            Nodominate_Sort = J_Fast_Sort.Fast_Nodominate_Sort(Merge_Population)

            Next_Population = []  # 从子父代混合种群中选取前200个作为下一代种群
            i = 0
            # 按照Parato等级依次添加
            while (len(Next_Population) + len(Nodominate_Sort[i])) <= Pop.Pop_Size:
                for j in Nodominate_Sort[i]:
                    Next_Population.append(Merge_Population[j])
                i = i + 1

            # for m in Nodominate_Sort[i]:    # 把最后一层先加上
            #     Next_Population.append(Merge_Population[m])
            # Next_Population = Next_Population[0:Pop.Pop_Size]   # 生成新种群

            Di = J_Fast_Sort.Modified_Crowding_Distance(Nodominate_Sort[i], Merge_Population)  # 拥挤度 未排序

            Temp_Choose = []  # 选解
            for j in range(len(Di)):
                Temp = []
                Temp.append(Di[j])
                Temp.append(Nodominate_Sort[i][j])
                Temp_Choose.append(Temp)

            Temp_Choose = sorted(Temp_Choose)

            Index = len(Temp_Choose) - 1
            while len(Next_Population) < Pop.Pop_Size:
                Next_Population.append(Merge_Population[Temp_Choose[Index][1]])

            Pop.Update_Pop(Next_Population)
            Pop.Determine_Nodominate_Ship()

            gen += 1

        return Pop


if __name__ == "__main__":
    print(" __name__ 1 ")

if __name__ == "__main__":
    print(" __name__ 2")
    # 订单数据结构的处理结果
    PS_Chromosome = ['PC_0_01_0', 'PC_0_02_0', 'PC_0_03_0', 'PC_0_02_1', 'PC_0_02_2']  # 为原始订单
    LS_Chromosome = ['1#', '2#', '1#', '2#', '1#']

    DataStructure = Load_Process_Map_Me()
    PC_DataStructure = DataStructure.PC_DataStructure_Initial(B_Data.Order1)
    Resource_DataStructure = DataStructure.Resource_DataStructure_Initial()

    Idv1 = Individual()
    Idv1.Update_Chromosome(PS_Chromosome, LS_Chromosome, PC_DataStructure, Resource_DataStructure)


    # 画图
    Idv2 = Individual()
    Idv2.Update_Chromosome_Draw(PS_Chromosome, LS_Chromosome)
    S_Draw.Draw_Gatt_Me(Idv2, PC_DataStructure, Resource_DataStructure)


